<template>
  <div class="mod-config editViewContainer">

      <template >
        <table-view ref="view"
                    :searchAttrs="searchAttrs"
                    :primaryKey="primaryKey"
                    :listApiName="listApiName"
                    :deleteApiName="deleteApiName"
                    :showAttrs="showAttrs"
                    :exportApiName="exportApiName"

                    :optionWidth="150"
        >
          <template  v-slot:header>
          </template>
          <template v-slot:foot="{scope}">

          </template>
        </table-view>
      </template>

  </div>
</template>

<script>
import AddOrUpdate from './orderdetail-add-or-update'
import tableView from '@/components/tableView'
import basicMixins from '@/utils/commonViewMixins'
import ProductinfoAddOrUpdate from '@/views/modules/product/productinfo-add-or-update'
import ProductplannoticeAddOrUpdate from '@/views/modules/product/productplannotice-add-or-update'
import OrderModelAddOrUpdate from './ordermodel-add-or-update'
import ClientInfoAddOrUpdate from '@/views/modules/client/clientinfo-add-or-update'
import DeepCategoryAddOrUpdate from '@/views/modules/deep/deepcategory-add-or-update'
import {mRequest} from '@/utils/request'
import {exportHandle, isAuth} from '@/utils'
import ModelInfoAddOrUpdate from '@/views/modules/model/modelinfo-add-or-update'
import treeView from '@/components/treeView'

export default {
  mixins: [basicMixins],
  data() {
    return {
      isAuth: isAuth,
      status: 0,
      bigMouth: '',
      boxOrderStatusRadio: 0,
      boxAuditStatusRadio: 0,
      productDeviceRadio: 0,
      orderIssuesNumber: 0,
      modelApprovalRadio: 0,
      groupNumber: 0,
      modelId: '',
      modelShelfId: '',
      dialogVisible: false,
      modelInfoVisible: false,
      orderIssuesNumberVisible: false,
      boxOrderDialogVisible: false,
      productPlanNoticeVisible: false,
      productInfoVisible: false,
      clientInfoVisible: false,
      deepCategoryInfoVisible: false,
      bigMouthVisible: false,
      orderPlanNoticeVisible: false,
      boxAuditDialogVisible: false,
      productDeviceDialogVisible: false,
      sendOrderLeadApprovalDialogVisible: false,
      modelApprovalDialogVisible: false,
      orderModelVisible: false,
      productDeviceList: [],
      deviceIdList: [],
      orderDetailList: [],
      productInfoList: [],
      modelInfoList: [],
      tableListData: [],
      deepCategoryList: [],
      productDeviceTreeList: [],
      modelShelfList: [],
      sendOrderLeadApproval: 11,
      sendOrderOpinion: '',
      modelOpinion: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      sysUserList: [],
      deviceId: '',
      onlineTime: '',
      searchAttrs: {
        productId: {
          type: 'autoComplete',
          placeholder: '请选择产品',
          querySearchApiName: 'productApi/getProductInfoList'
        },

        clientId: {
          label: '客户名称',
          clearable: true,
          type: 'autoComplete',
          placeholder: '请输入客户名称',
          querySearchApiName: 'clientApi/getClientList'
        },

        dateTimeRange: {
          type: 'date',
          dateType: 'daterange',
          clearable: true
        }
      },
      primaryKey: 'id',
      listApiName: 'orderApi/getOrderDetailPageByProductIdAndPutInTime',
      deleteApiName: 'orderApi/deleteOrderDetail',
      importApiName: 'orderApi/importOrderDetail',
      saveAuthName: 'order:orderdetail:save',
      deleteAuthName: 'order:orderdetail:delete',
      boxOrderAuthName: 'order:orderdetail:boxOrder',
      modelAuthName: 'order:orderdetail:modelAuth',
      //getProductInStorageCountByOrderExport
      sendOrderLeadApprovalAuthName: 'order:orderdetail:sendOrderLeadApproval',
      exportApiName: 'productApi/getProductInStorageCountByOrderExport',
      showAttrs: {
        productNo: {
          label: '产品编号',
          type: 'linkLabel',
          routerName: 'product-product-info-all',
          searchKey: 'productNo',
          width:130
        },
        productWeight: {label: '克数', type: 'tag'},
        boxZhiNumber: {label: '箱只'},
        orderProductNumber: {label: '订单数量'},
/*        boxZhiNumber: {label: '箱只'},
        boxNumber: {label: '箱数'},
        scrapNumber: {label: '零头'},*/
        putInStorageValue: {label: '入库数量', type: 'tag',width:130},
        leaveStorageValue: {label: '出库数量'},
        statisticsValue: {label: '剩余数量',type: 'tag'},
        location: {label: '存放位置'},
        putInTime: {label: '日期', type: 'date',width:110},
        clientName: {
          label: '客户名称',
          type: 'linkLabel',
          routerName: 'client-clientinfo',
          searchKey: 'clientName'
        },
        // },
        remark: {label: '备注'},
        orderNo: {label: '订单编号'}
      }
    }
  },

  methods: {


    openOrderStatusDialog(order) {
      this.dialogVisible = true
      this.modifyOrder = order
      const {status} = order

      this.status = status
    },
    modifyOrderStatus() {
      if (this.modifyOrder.status === this.status) {
        return
      }
      this.modifyOrder.status = this.status

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.status = 0
        this.dialogVisible = false
        this.getDataList()
      })
    },
    modifyBigMouth() {
      if (this.modifyOrder.bigMouth === this.bigMouth) {
        return
      }
      this.modifyOrder.bigMouth = this.bigMouth

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.bigMouth = ''
        this.bigMouthVisible = false
        this.getDataList()
      })
    },
    openBigMouthDialog(order) {
      this.bigMouthVisible = true
      this.modifyOrder = order
      const {bigMouth} = order

      this.bigMouth = bigMouth
    },

    openBoxOrderStatusDialog(order) {
      this.boxOrderDialogVisible = true
      this.modifyOrder = order
      const {boxOrderStatus} = order

      this.boxOrderStatusRadio = boxOrderStatus
    },
    modifyBoxOrderStatus() {
      if (this.modifyOrder.boxOrderStatus === this.boxOrderStatusRadio) {
        return
      }
      this.modifyOrder.boxOrderStatus = this.boxOrderStatusRadio

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.boxOrderStatusRadio = 0
        this.boxOrderDialogVisible = false
        this.getDataList()
      })
    },
    //纸箱审核
    openBoxAuditStatusDialog(order) {
      this.boxAuditDialogVisible = true

      this.modifyOrder = order
      const {boxAudit} = order

      this.boxAuditStatusRadio = boxAudit
    },
    modifyBoxAuditStatus() {
      if (this.R.equals(this.modifyOrder.boxAudit, this.boxAuditStatusRadio)) {
        this.modifyOrder = null
        this.boxAuditStatusRadio = 0
        this.boxAuditDialogVisible = false
        return
      }
      this.modifyOrder.boxAudit = this.boxAuditStatusRadio

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.boxAuditStatusRadio = 0
        this.boxAuditDialogVisible = false
        this.getDataList()
      })
    },
    //派单审核
    openSendOrderDialog(order) {
      this.sendOrderOpinion = null
      this.sendOrderLeadApprovalDialogVisible = true

      this.modifyOrder = order
      const {sendOrderLeadApproval} = order

      this.sendOrderLeadApproval = sendOrderLeadApproval
      this.sendOrderOpinion = order.sendOrderOpinion
    },
    modifySendOrderApproval() {
      if (this.modifyOrder.sendOrderLeadApproval === this.sendOrderLeadApproval) {
        return
      }
      this.modifyOrder.sendOrderLeadApproval = this.sendOrderLeadApproval
      this.modifyOrder.sendOrderOpinion = this.sendOrderOpinion

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.sendOrderLeadApproval = 0
        this.sendOrderOpinion = null
        this.sendOrderLeadApprovalDialogVisible = false
        this.getDataList()
      })
    },
    modifyOrderIssuesNumber() {
      if (this.modifyOrder.orderIssuesNumber === this.orderIssuesNumber) {
        return
      }
      this.modifyOrder.orderIssuesNumber = this.orderIssuesNumber

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.orderIssuesNumber = 0
        this.orderIssuesNumberVisible = false
        this.getDataList()
      })
    },
    //模具审核
    openModelApprovalDialog(order) {

      this.initModelInfoList()
      this.initModelShelfList()
      this.modelApprovalDialogVisible = true

      this.modifyOrder = order
      const {modelApproval, modelOpinion,modelId,modelShelfId,groupNumber} = order

      this.modelApprovalRadio = modelApproval
      this.modelOpinion = modelOpinion
      this.modelId = modelId
      this.modelShelfId = modelShelfId
      this.groupNumber = groupNumber
    },
    modifyModelApprovalStatus() {

      mRequest('modelApi/updateModelInfoByOrderInfo', {
        'id':this.modelId,
        'productId':this.modifyOrder.productId,
        'shelfId': this.modelShelfId
      }, () => {
        this.modelId = ''
        this.modelShelfId = ''

      })
      if (this.R.equals(this.modifyOrder.modelApproval, this.modelApprovalRadio)&&this.R.equals(this.modifyOrder.modelShelfId, this.modelShelfId)) {
        this.modifyOrder = null
        this.modelOpinion = null
        this.modelApprovalRadio = 0
        this.modelShelfId = ''
        this.modelApprovalDialogVisible = false
        return
      }
      this.modifyOrder.modelApproval = this.modelApprovalRadio
      this.modifyOrder.modelOpinion = this.modelOpinion
      this.modifyOrder.groupNumber = this.groupNumber

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.modelOpinion = null
        this.modelApprovalRadio = 0
        this.modelApprovalDialogVisible = false
        this.getDataList()
      })
    },
    openProductPlanNotice(id) {
      this.productPlanNoticeVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.productPlanNotice)) {
          this.$refs.productPlanNotice.init(null, {orderDetailId: id}, () => {
            this.$router.push({name: 'product-productplannotice'})
          })
        }
      })
    },
    openProductDeviceDialog(order) {
      this.productDeviceDialogVisible = true

      this.modifyOrder = order
      const {deviceId} = order

      this.productDeviceRadio = deviceId
    },
    modifyProductDeviceStatus() {
      if (this.modifyOrder.deviceId === this.productDeviceRadio) {
        return
      }
      this.modifyOrder.deviceId = this.productDeviceRadio

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.productDeviceRadio = 0
        this.productDeviceDialogVisible = false
        this.getDataList()
      })
    },
    openProductInfoDialog(cb) {
      this.productInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.productInfo)) {
          this.$refs.productInfo.init(null, {status: 1}, cb)
        }
      })
    },
    openClientInfoDialog(cb) {
      this.clientInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.clientInfo)) {
          this.$refs.clientInfo.init(null, {status: 1}, cb)
        }
      })
    },
    openDeepCategoryInfoDialog(cb) {
      this.deepCategoryInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.deepCategory)) {
          this.$refs.deepCategory.init(null, {status: 1}, cb)
        }
      })
    },
    openOrderIssuesNumberDialog(order) {
      this.orderIssuesNumberVisible = true
      this.modifyOrder = order
      const {orderIssuesNumber} = order

      this.orderIssuesNumber = orderIssuesNumber
    },
    openOrderModelDialog() {
      this.orderModelVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.orderModelInfo)) {
          this.$refs.orderModelInfo.init(null)
        }
      })
    },
    exportPlantFunc(rows) {
      this.onlineTime = ''
      this.orderPlanNoticeVisible = true
      this.deviceId = rows[0].deviceId
      this.initProductDeviceListByParent()
      this.initOrderDetailList()
      this.initProductInfoList()
      this.initDeepCategoryList()
      this.initModelInfoList()
      this.initSysUserInfoList()
      this.tableListData = rows

    },

    initSysUserInfoList() {
      mRequest('systemApi/listAllUser', {}, (res) => {
        this.sysUserList = this.R.pathOr([], ['data', 'list'], res)
      })
    },

    initProductDeviceListByParent() {
      mRequest('productApi/getProductDeviceListByParent', {}, (res) => {
        this.productDeviceList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initOrderDetailList() {
      mRequest('orderApi/getOrderDetailList', {}, (res) => {
        this.orderDetailList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initProductInfoList() {
      mRequest('productApi/getProductInfoList', {}, (res) => {
        this.productInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initDeepCategoryList() {
      mRequest('deepApi/deepCategoryList', {}, (res) => {
        this.deepCategoryList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initModelInfoList(attrName = 'modelInfoId') {
      mRequest('modelApi/getModelInfoList', {}, (res) => {
        this.modelInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initModelShelfList(attrName = 'modelShelfId') {
      mRequest('modelApi/getModelShelfList', {}, (res) => {
        this.modelShelfList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    exportOrderNotFinish() {
      exportHandle('productApi/getProductOrderNotFinishExport', {})

    },

    updatePlanOrderStatus() {

      const ids = this.tableListData.map(item => {
        return item[this.primaryKey]
      })

      mRequest('orderApi/updatePlanOrderStatus', {
        'onlineTime': this.onlineTime,
        'deviceId': this.deviceId,
        'tableListData': this.tableListData
      }, (res => {
        let data = res.data
        if (data && data.code == 0) {

          this.$confirm('是否直接导出改计划?', '提示', {
            confirmButtonText: '导出',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            exportHandle('productApi/getProductPlanNoticeExport', {orderDetailIdList: ids})
          }).catch(() => {
          })

          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.orderPlanNoticeVisible = false
              this.getDataList()
              this.$emit('refreshDataList')
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      }))
    }
  },
  components: {
    tableView
  }
}
</script>
